package top.ochiamalu.aioj.factory;

import org.springframework.stereotype.Component;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.strategy.judge.CodeExecutor;

import java.util.HashMap;
import java.util.Map;

/**
 * 代码执行器工厂类
 *
 * @author ochiamalu
 * @date 2025/06/20
 */
@Component
public class CodeExecutorFactory {

    private final Map<String, CodeExecutor> executorMap = new HashMap<>();

    /**
     * 注册代码执行器
     *
     * @param language 语言
     * @param executor 执行器
     */
    public void register(String language, CodeExecutor executor) {
        executorMap.put(language, executor);
    }

    /**
     * 获取代码执行器
     *
     * @param language 语言
     * @return 代码执行器
     */
    public CodeExecutor getExecutor(String language) {
        CodeExecutor executor = executorMap.get(language);
        if (executor == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的语言类型: " + language);
        }
        return executor;
    }
} 